package com.study.talk.ui.app.other

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.gson.reflect.TypeToken
import com.study.talk.R
import com.study.talk.databinding.ActivityImagePreviewBinding
import com.study.talk.di.AppModule
import com.study.talk.ui.app.common.AnalyzeImageFragment
import com.study.talk.ui.view.ZoomImageView
import com.study.talk.utils.extras.getIntParam
import com.study.talk.utils.extras.getStrParam
import com.study.talk.utils.extras.imageUrl
import com.study.talk.utils.extras.toObj
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart.ChatCompletionContentPartImageURL
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.indicator.CircleIndicator

class ImagePreviewActivity : AppCompatActivity() {

    companion object {
        const val IMAGES = "images"
        const val INDEX = "index"
    }

    private val images by lazy {
        getStrParam(IMAGES).toObj<List<String>>(object :
            TypeToken<List<String>>() {}.type)
    }

    private val index by lazy { getIntParam(INDEX) }

    private val binding by lazy { ActivityImagePreviewBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
    }

    private fun initView() {
        binding.imagePreview.apply {
            visibility = View.INVISIBLE
            setAdapter(ImageAdapter(images), false)
            setPageTransformer(null)
            indicator = CircleIndicator(this@ImagePreviewActivity)
            addBannerLifecycleObserver(this@ImagePreviewActivity)
            isAutoLoop(false)
            setCurrentItem(index, false)
            visibility = View.VISIBLE
        }
        binding.smartImageAnalyzeFloatBtn.setOnClickListener {
            val imageUrl = images[ binding.imagePreview.currentItem].imageUrl()
            val fragment = AnalyzeImageFragment(
               mutableListOf(buildImageMsg(imageUrl))
            )
            fragment.show(supportFragmentManager,"analyzePictureFragment")
        }
    }

    private fun buildImageMsg(imageUrl: String): ChatMessage {
        return ChatMessage.builder().role(ChatMessageRole.USER).multiContent(
            listOf(
                ChatCompletionContentPart.builder().type("text").text("上述图片的内容是什么?")
                    .build(),
                ChatCompletionContentPart.builder().type("image_url")
                    .imageUrl(ChatCompletionContentPartImageURL(imageUrl))
                    .build()
            )
        ).build()
    }

    inner class ImageAdapter(data: List<String>) :
        BannerAdapter<String, ImageAdapter.BannerViewHolder>(data) {
        //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
        override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.image_preview_item, parent, false)
            return BannerViewHolder(view)
        }

        override fun onBindView(
            holder: BannerViewHolder, data: String, position: Int, size: Int
        ) {
            Glide.with(this@ImagePreviewActivity).load(AppModule.getWebUrl() + data)
                .into(holder.imageView)
        }

        inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val imageView: ZoomImageView = view.findViewById(R.id.image_preview_item_image)
        }
    }
}